/*
 * filter.cpp
 *
 * Created: 01.05.2012 15:17:36
 *  Author: maxim.karban@gmail.com
 */ 

#include <stdint.h>

int32_t filterA1_lpf(int32_t NewSample) {
#define Koeff 3
#define RKoeff 1024
  
    static int32_t AF0;
    static int32_t AFprev;
    AF0 = (int32_t)(AFprev * (int32_t)Koeff);
    AF0 = AF0 >> 10;
    static int32_t AF1;
    AF1 = (int32_t)((NewSample<<3) * ((int32_t)RKoeff - (int32_t)Koeff));
    AF1 = AF1 >> 10;
    AFprev = (AF0 + AF1);
    return (int32_t) (AFprev>>3);
}

int32_t filterA1_avg(int32_t NewSample) {
#define avg_samples 50
    static int32_t avg;
    static int32_t x[avg_samples]; //input samples
    avg = 0;
    for(int i = 0; i < avg_samples; i++){
      if(i<avg_samples-1){
      x[i] = x[i+1];
      }else{
        x[i] = NewSample;
      }
      avg += x[i]; 
    }
    avg /= avg_samples;
  
    return (int32_t) avg;
}
